package com.koron.common.core.util;

import cn.hutool.extra.spring.SpringUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.org.OrgBean;
import com.koron.bean.util.RedisUtils;
import com.koron.common.core.business.system.feign.SystemFeignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author qingfeng
 * @description: 组织缓存
 * @date 2021/8/4 14:35
 */
@Component
public class OrgCacheUtils {

    private static final String key_prefix = "eam:org:id:";

    public OrgBean getOrgById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        OrgBean orgBean = (OrgBean) RedisUtils.get(key_prefix + id);
        // 缓存为空，再调用feign，再塞入缓存
        if (orgBean == null) {
            SystemFeignService systemFeignService = SpringUtils.getBean(SystemFeignService.class);
            orgBean = systemFeignService.querySingleOrg(id).getData();
            RedisUtils.set(key_prefix + id, orgBean, 6 * 60 * 60);
        }
        return orgBean;
    }

    public String getOrgNameById(String id) {
        OrgBean org = getOrgById(id);
        return org == null ? "" : org.getName();
    }

    public static OrgBean queryDeptByOrgId(String orgId) {
        if (StringUtils.isBlank(orgId)) {
            return null;
        }
        OrgBean orgBean = (OrgBean) RedisUtils.get(key_prefix + orgId);
        // 缓存为空
        return orgBean;
    }

    public static List<OrgBean> queryOrgListByParentId(String orgId) {
        OrgBean orgBean = new OrgBean();
        orgBean.setParent(orgId);
        SystemFeignService systemFeignService = SpringUtils.getBean(SystemFeignService.class);
        Response<List<OrgBean>> listResponse = SpringUtil.getBean(SystemFeignService.class).queryNextOrgList(orgBean);
        return listResponse.getData();
    }

}
